package com.shushan.home.bean;

import com.android.log.LogUtil;
import com.android.net.bean.NetBaseBean;
import com.shushan.base.Constants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MainDataBean
 * @Desciption: 首页bean
 * @author: yichaohua
 * @date: 2018-04-16
 */
public class MainDataBean extends NetBaseBean {

    private static final String TAG = "MainDataBean";

    private List<LearnByDoBean> chineseByDoList = new ArrayList<LearnByDoBean>();
    private List<LearnByDoBean> mathByDoList = new ArrayList<LearnByDoBean>();
    private List<LearnByDoBean> englishByDoList = new ArrayList<LearnByDoBean>();

    private List<RewardTimeBean> rewardTimeList = new ArrayList<RewardTimeBean>();
    private List<RewardToyBean> rewardToyList = new ArrayList<RewardToyBean>();

    private List<BannerDataBean> bannerDataBeanList = new ArrayList<>();
    private List<StarsDataBean> starsDataBeanList = new ArrayList<>();

    @Override
    public void initByJson(JSONObject jsonObject) throws JSONException {

        LogUtil.i(TAG,"jsonObject=" + jsonObject);

        JSONArray bannerArray = jsonObject.optJSONArray("banner");
        for(int i=0;i<bannerArray.length();i++){
            JSONObject jobj = bannerArray.getJSONObject(i);
            BannerDataBean bannerDataBean = new BannerDataBean();
            bannerDataBean.initByJson(jobj);

            bannerDataBeanList.add(bannerDataBean);
        }

        JSONArray starArray = jsonObject.optJSONArray("stars");
        for(int i=0;i<starArray.length();i++){
            JSONObject jobj = starArray.getJSONObject(i);
            StarsDataBean starsDataBean = new StarsDataBean();
            starsDataBean.initByJson(jobj);

            starsDataBeanList.add(starsDataBean);
        }

        JSONObject learnJson = jsonObject.optJSONObject("learn");

        JSONObject learnRewardTimeJson = learnJson.optJSONObject("reward_time");
        RewardTimeBean learnRewardTimeBean = new RewardTimeBean();
        if(learnRewardTimeJson != null && learnRewardTimeJson.length() > 0){

            learnRewardTimeBean.initByJson(learnRewardTimeJson);
        }

        JSONObject learnRewardToyJson = learnJson.optJSONObject("reward_toy");
        RewardToyBean learnRewardToyBean = new RewardToyBean();
        if(learnRewardToyJson != null && learnRewardToyJson.length() > 0){

            learnRewardToyBean.initByJson(learnRewardToyJson);
        }

        JSONArray chineseJsonArray = learnJson.optJSONArray(Constants.SUBJECT_CHINESE_ID + "");
        if(chineseJsonArray != null){
            for (int i = 0;i < chineseJsonArray.length();i++){
                JSONObject json = chineseJsonArray.optJSONObject(i);
                LearnByDoBean learnByDoBean = new LearnByDoBean();
                learnByDoBean.initByJson(json);
                learnByDoBean.setRewardTimeBean(learnRewardTimeBean);
                learnByDoBean.setRewardToyBean(learnRewardToyBean);

                chineseByDoList.add(learnByDoBean);
            }
        }

        JSONArray mathJsonArray = learnJson.optJSONArray(Constants.SUBJECT_MACH_ID + "");
        if(mathJsonArray != null){
            for (int i = 0;i < mathJsonArray.length();i++){
                JSONObject json = mathJsonArray.optJSONObject(i);
                LearnByDoBean learnByDoBean = new LearnByDoBean();
                learnByDoBean.initByJson(json);
                learnByDoBean.setRewardTimeBean(learnRewardTimeBean);
                learnByDoBean.setRewardToyBean(learnRewardToyBean);

                mathByDoList.add(learnByDoBean);
            }
        }

        JSONArray englishJsonArray = learnJson.optJSONArray(Constants.SUBJECT_ENGLISH_ID + "");
        if(englishJsonArray != null){
            for (int i = 0;i < englishJsonArray.length();i++){
                JSONObject json = englishJsonArray.optJSONObject(i);
                LearnByDoBean learnByDoBean = new LearnByDoBean();
                learnByDoBean.initByJson(json);
                learnByDoBean.setRewardTimeBean(learnRewardTimeBean);
                learnByDoBean.setRewardToyBean(learnRewardToyBean);

                englishByDoList.add(learnByDoBean);
            }
        }

        JSONArray rewardTimeJsonArray = jsonObject.optJSONArray("reward_time");
        if(rewardTimeJsonArray != null){
            for (int i = 0;i < rewardTimeJsonArray.length();i++){
                JSONObject json = rewardTimeJsonArray.optJSONObject(i);
                RewardTimeBean rewardTimeBean = new RewardTimeBean();
                rewardTimeBean.initByJson(json);

                rewardTimeList.add(rewardTimeBean);
            }
        }

        JSONArray rewardToyJsonArray = jsonObject.optJSONArray("reward_toy");
        if(rewardToyJsonArray != null){
            for (int i = 0;i < rewardToyJsonArray.length();i++){
                JSONObject json = rewardToyJsonArray.optJSONObject(i);
                RewardToyBean rewardToyBean = new RewardToyBean();
                rewardToyBean.initByJson(json);

                rewardToyList.add(rewardToyBean);
            }
        }
    }

    public boolean isEmpty(){
        if(chineseByDoList.size() == 0 && mathByDoList.size() == 0
                && englishByDoList.size() == 0 && rewardTimeList.size() == 0
                && rewardToyList.size() == 0){

            return true;
        }

        return false;
    }

    public List<LearnByDoBean> getChineseByDoList() {
        return chineseByDoList;
    }

    public List<LearnByDoBean> getMathByDoList() {
        return mathByDoList;
    }

    public List<LearnByDoBean> getEnglishByDoList() {
        return englishByDoList;
    }

    public List<RewardTimeBean> getRewardTimeList() {
        return rewardTimeList;
    }

    public List<RewardToyBean> getRewardToyList() {
        return rewardToyList;
    }

    public List<BannerDataBean> getBannerDataBeanList() {
        return bannerDataBeanList;
    }

    public List<StarsDataBean> getStarsDataBeanList() {
        return starsDataBeanList;
    }

}
